<?php


namespace app\system\controller;

class News extends AdminBase
{
	public function getlist()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\NewsService::getlist($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function getinfo($id)
	{
		$options = ["id" => $id];
		$result = \app\system\service\NewsService::getinfo($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function add()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["cid" => "require", "title" => "require", "content" => "require", "sort" => "require", "type" => "require"], ["cid.require" => "cid 不能为空", "title.require" => "title 不能为空", "content.require" => "content 不能为空", "sort.require" => "sort 不能为空", "type.require" => "type 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\NewsService::add($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "cid" => "require", "title" => "require", "content" => "require", "sort" => "require", "type" => "require"], ["id.require" => "id 不能为空", "cid.require" => "cid 不能为空", "title.require" => "title 不能为空", "content.require" => "content 不能为空", "sort.require" => "sort 不能为空", "type.require" => "type 不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\NewsService::edit($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del()
	{
		$options = $this->request->post();
		$result = \app\system\service\NewsService::del($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_classify_list()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\NewsService::get_classify_list($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function add_classify()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["name" => "require", "type" => "require", "sort" => "require"], ["name.require" => "请输入 name", "sort.require" => "请输入 sort", "type.require" => "请输入 type"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\NewsService::add_classify($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function get_classify_info($id)
	{
		$options = ["id" => $id];
		$result = \app\system\service\NewsService::get_classify_info($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit_classify()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "name" => "require", "type" => "require", "sort" => "require"], ["id.require" => "分类id不能为空！", "name.require" => "请输入 name", "sort.require" => "请输入 sort", "type.require" => "请输入 type"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\NewsService::edit_classify($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "编辑成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del_classify($id)
	{
		$options = ["id" => $id];
		$result = \app\system\service\NewsService::del_classify($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}